package algorithm;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

public class leetcode_0870 {
    public int[] advantageCount(int[] nums1, int[] nums2) {
        PriorityQueue<int[]> priorityQueue = new PriorityQueue<>(new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o2[1]-o1[1];
            }
        });
        for(int i=0;i<nums2.length;++i){
            priorityQueue.add(new int[]{i,nums2[i]});
        }
        Arrays.sort(nums1);
        int left = 0;
        int right = nums1.length-1;
        int[] result = new int[nums1.length];
        while(priorityQueue.size()>0){
            int[] tmp = priorityQueue.poll();
            if(tmp[1]>=nums1[right]){
                result[tmp[0]]=nums1[left];
                left++;
            } else{
                result[tmp[0]]=nums1[right];
                right--;
            }
        }
        return result;
    }
}
